package com.ymxk.timestar.activity;

import android.animation.Animator;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowInsets;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.ymxk.timestar.view.drag.utils.AnimationUtils;

import java.util.ArrayList;

/**
 * view的移动效果、LocationInWindowActivity
 */
public class LocationInWindowActivity extends AppCompatActivity {

    private ConstraintLayout mMain;
    private Button mBtnMove;
    private TextView mTvCenter;
    private final String TAG = LocationInWindowActivity.class.getSimpleName();
    private Button mBtnGo;
    private int centerX;
    private int centerY;
    private int moveX;
    private int moveY;
    private LinearLayout mLlBtn;
    private Button mBtnMove1;
    private Button mBtnMove2;
    private View mVVerLine;
    private View mVHorizontalLine;
    private Button mBtnGo1;
    private Button mBtnGo2;
    private int currentX;
    private int currentY;
    private Button mBtnGo3;
    private int barHeight;

    public static void jumpTo(Context context) {
        Intent intent = new Intent(context, LocationInWindowActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        barHeight = getStatusBarHeight();
        Log.e(TAG, "onCreate: barHeight--" + barHeight);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_view_move);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            Log.e(TAG, "onCreate: systemBars.top--" + systemBars.top);
            Log.e(TAG, "onCreate: systemBars.bottom--" + systemBars.bottom);
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        initView();

        mTvCenter.post(this::onInitCenterLocation);
//        mBtnMove.post(this::onInitButtonLocation);
//        mBtnMove.post(() -> onInitButtonLocation(mBtnMove));
//        mBtnMove1.post(() -> onInitButtonLocation(mBtnMove1));
//        mBtnMove2.post(() -> onInitButtonLocation(mBtnMove2));
        mLlBtn.post(this::getViewGroupLocation);

    }

    private void getViewGroupLocation() {
        int[] location = new int[2];
        mLlBtn.getLocationInWindow(location);
        int x = location[0];
        int y = location[1];
        Log.e(TAG, "mLlBtn: x=" + x + ",y=" + y);
        //mLlBtn: x=0,Y=107
    }

    private void onInitButtonLocation(Button button) {
        onGetButtonLocation(button);
        //onButtonMove: x=0,moveY=107
        translationToCenter(button);
    }

    private void onGetButtonLocation(Button button) {
        int[] location = new int[2];
        button.getLocationInWindow(location);
        currentX = location[0];
        currentY = location[1];
        Log.e(TAG, button.getText() + ": x=" + currentX + ",y=" + currentY);
    }

    private void onGetButtonCenterLocation(Button button) {
        int[] location = new int[2];
        button.getLocationInWindow(location);
        moveX = location[0] + button.getWidth() / 2;
        moveY = location[1] + button.getHeight() / 2;
        Log.e(TAG, "onButtonMove: x=" + moveX + ",moveY=" + moveY);
    }

    private void onInitCenterLocation() {
        int width = mTvCenter.getWidth();
        int height = mTvCenter.getHeight();
//        Log.e(TAG, "onInitCenterLocation: w=" + width + "h=" + height);

        int[] location = new int[2];
        mTvCenter.getLocationInWindow(location);
//        centerX = location[0] + width / 2;
//        centerY = location[1] + height / 2;
//        centerX = location[0];
//        centerY = location[1];
//        Log.e(TAG, "onInitCenterLocation: x=" + centerX + ",y=" + centerY);
        //onInitCenterLocation: x=240,y=888

        PointF pointF = new PointF(location[0], location[1]);
        PointF pointF1 = new PointF(location[0] + width, location[1]);
        PointF pointF2 = new PointF(location[0], location[1] + height);
        PointF pointF3 = new PointF(location[0] + width, location[1] + height);
        list.clear();
        list.add(pointF);
        list.add(pointF1);
        list.add(pointF2);
        list.add(pointF3);

    }

    ArrayList<PointF> list = new ArrayList<>();

    private void initView() {
        mMain = findViewById(R.id.main);
        mBtnMove = findViewById(R.id.btnMove);
        mBtnMove.setOnClickListener(this::onButtonMove);
        mTvCenter = findViewById(R.id.tvCenter);
        mTvCenter.setOnClickListener(v -> onInitCenterLocation());

        mBtnGo = findViewById(R.id.btnGo);
//        mBtnGo.setOnClickListener(this::moveToCenter);
//        mBtnGo.setOnClickListener(this::translationToCenter);
        mLlBtn = (LinearLayout) findViewById(R.id.llBtn);
        mBtnMove1 = (Button) findViewById(R.id.btnMove1);
        mBtnMove2 = (Button) findViewById(R.id.btnMove2);
        mVVerLine = (View) findViewById(R.id.vVerLine);
        mVHorizontalLine = (View) findViewById(R.id.vHorizontalLine);
        mBtnGo1 = (Button) findViewById(R.id.btnGo1);
        mBtnGo2 = (Button) findViewById(R.id.btnGo2);
        mBtnGo3 = (Button) findViewById(R.id.btnGo3);

        mBtnGo.setOnClickListener(v -> {
            translation(list.get(0));
        });
        mBtnGo1.setOnClickListener(v -> {
            translation(list.get(1));
        });
        mBtnGo2.setOnClickListener(v -> {
            translation(list.get(2));
        });
        mBtnGo3.setOnClickListener(v -> {
            translation(list.get(3));
        });
    }

    private void translation(PointF pointF) {
        mLlBtn.animate()
                .translationX(pointF.x)
                .translationY(pointF.y - barHeight)
                .setListener(new AnimationUtils.AnimationEndListener() {
                    @Override

                    public void onAnimationEnd(@NonNull Animator animation) {
                        Log.e(TAG, "onAnimationEnd: ");
                    }
                })
                .start();

    }

    private void moveToCenter(View v) {
        int x = centerX - moveX;
        int y = centerY - moveY;
        mLlBtn.animate()
                .x(x)
                .y(y)
                .setListener(new AnimationUtils.AnimationEndListener() {
                    @Override

                    public void onAnimationEnd(@NonNull Animator animation) {
                        Log.e(TAG, "onAnimationEnd: ");
                        getViewGroupLocation();
                    }
                })
                .start();
    }

    private void translationToCenter(View v) {
//        int x = centerX - moveX;
//        int y = centerY - moveY;
        Log.e(TAG, "translationToCenter: currentX=" + currentX + ",currentY=" + currentY);

        int x = centerX - currentX;
        int y = centerY - currentY;
        Log.e(TAG, "translationToCenter: x=" + x + ",y=" + y);
        mLlBtn.animate()
                .translationX(x)
                .translationY(y)
                .setListener(new AnimationUtils.AnimationEndListener() {
                    @Override

                    public void onAnimationEnd(@NonNull Animator animation) {
                        Log.e(TAG, "onAnimationEnd: ");
                        getViewGroupLocation();
                        //mBtnMove1.post(() -> onInitButtonLocation(mBtnMove1));
                        onGetButtonLocation(mBtnMove1);
                    }
                })
                .start();
    }

    private void onButtonMove(View v) {
        Log.e(TAG, "onButtonMove: ");
        onInitButtonLocation((Button) v);
    }

//    private int getStatusBarHeight(Activity activity) {
//        WindowInsets windowInsets = activity.getWindow().getDecorView().getRootWindowInsets();
//        return windowInsets.getSystemWindowInsetTop();
//    }

    public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
}